package com.xingkeduo.pojo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xingkeduo.pojo.query.ScreenMapper;
import com.xingkeduo.pojo.table.Screen;
import com.xingkeduo.util.ScreenMapUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

/**
 * @author chi  2019-07-03 10:57
 **/
@Service
public class ScreenService {

    @Resource
    private ScreenMapper screenMapper;

    public boolean insertOrUpdate(Screen screen) {

        if (screen.getId() > 0) {
            screenMapper.updateById(screen);
        } else {
            screenMapper.insert(screen);
        }
        return true;
    }


    public Optional<Screen> getByClientId(String clientId){
        Screen screen = screenMapper.selectOne(new QueryWrapper<Screen>().lambda().eq(Screen::getClientId, clientId).last("LIMIT 1 for update"));
        return Optional.ofNullable(screen);
    }


    public Optional<Screen> getBySimSerialNumber(String simSerialNumber){
        Screen screen = screenMapper.selectOne(new QueryWrapper<Screen>().lambda().eq(Screen::getSimSerialNumber, simSerialNumber).last("LIMIT 1 for update"));
        return Optional.ofNullable(screen);
    }

    public Optional<List<Screen>> getByStoreId(String storeId){
        List<Screen> screens = screenMapper.selectList(new QueryWrapper<Screen>().lambda().eq(Screen::getStoreId, storeId));
        if(screens==null || screens.size()==0){
            return Optional.empty();
        }
        return Optional.of(screens);
    }

}
